package com.xyzk.diagnose.tools;

import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Locale;

import com.xyzk.diagnose.TApplication;

import android.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.widget.Toast;

public class DeviceLicence {
	private static WifiInfo mWifiInfo;
	private static ArrayList<String> mDeviceMac = new ArrayList<String>();

	static {
		//mDeviceMac.add("54:27:58:17:c3:29"); //testdevice1
		mDeviceMac.add("cc:4b:73:63:e4:d6"); //PAD 1
		mDeviceMac.add("cc:4b:73:a3:22:3e"); //PAD 2
		mDeviceMac.add("cc:4b:73:64:ae:de"); //PAD 3
		mDeviceMac.add("cc:4b:73:64:16:bc"); //PAD 4
		mDeviceMac.add("cc:4b:73:a3:ad:1c"); //PAD 5
		mDeviceMac.add("cc:4b:73:64:ce:60"); //PAD 6
		mDeviceMac.add("cc:4b:73:62:e3:83"); //PAD 7

		mDeviceMac.add("cc:4b:73:a3:12:74"); //PAD 8
		mDeviceMac.add("cc:4b:73:63:e4:84"); //PAD 9
		mDeviceMac.add("cc:4b:73:63:f6:e2"); //PAD 10
		mDeviceMac.add("cc:4b:73:74:78:64"); //PAD 11

		mDeviceMac.add("cc:4b:73:7f:0e:22"); //PAD 12
		mDeviceMac.add("cc:4b:73:7d:5d:a2"); //PAD 13
		mDeviceMac.add("cc:4b:73:63:13:7c"); //PAD 14
		mDeviceMac.add("cc:4b:73:64:af:e8"); //PAD 15
		mDeviceMac.add("cc:4b:73:64:3a:42"); //PAD 16
		mDeviceMac.add("cc:4b:73:a3:bf:32"); //PAD 17
		mDeviceMac.add("cc:4b:73:63:f4:60"); //PAD 18
		mDeviceMac.add("cc:4b:73:64:14:64"); //PAD 19
		mDeviceMac.add("cc:4b:73:64:b3:c2"); //PAD 20
		mDeviceMac.add("c4:41:37:0f:85:b7");  //TCU1
		mDeviceMac.add("54:65:03:21:95:5f");  //TCU2
		//20221012借个刷写平板
		mDeviceMac.add("64:c4:03:01:d5:76");  //TCU3
		mDeviceMac.add("64:c4:03:01:d4:c3");  //TCU4
		//
		mDeviceMac.add("c4:41:37:0f:85:b9");  //TCU5
		mDeviceMac.add("64:c4:03:01:d5:b7");  //TCU6
		mDeviceMac.add("e4:08:e7:6d:b2:c6");  //TCU7
		//old
		//mDeviceMac.add("c4:41:37:46:54:d");  //test
		mDeviceMac.add("64:c4:03:01:d3:05");  //kf1
		mDeviceMac.add("64:c4:03:01:d2:6e");	//kf2
		mDeviceMac.add("e4:08:e7:6d:b1:72"); //kf3
		mDeviceMac.add("64:c4:03:01:d2:a2");  //kf4
		mDeviceMac.add("64:c4:03:01:d2:b2");  //kf5
	}

	/**
	 * 校验是否是本公司设备
	 * @return 是/不是
	 */
	public static boolean checkDevice(){
		openWifi();
		String addr = getMacAddress();
		for(int i = 0; i < mDeviceMac.size(); i ++){
			if(mDeviceMac.get(i).equals(addr)){
				return true;
			}
		}
		return  true;
	}
	public static boolean CheckDeviceNew(Context context)
	{
		boolean isok = true;
		String result = "非法设备!";
		WifiAdmin m_wifi = new WifiAdmin(context);
		if(m_wifi.getState() < 2) //已经关闭，则打开
		{
			Toast.makeText(context, result, Toast.LENGTH_SHORT).show();
			m_wifi.openWifi();
			return false;
		}
		String defaultMac = "02:00:00:00:00:00";
		String addr = m_wifi.getMacAddress();
		if(addr.equals(defaultMac)) //高级版本
			addr = getWifiMacAddress();
		if(addr == null)
			addr = "no mac";
		for(int i = 0; i < mDeviceMac.size(); i ++)
		{
			if(addr.equals(mDeviceMac.get(i)))
			{
				result = "OK";
				isok = true;
				break;
			}
		}
		if(isok == false)
		{
			Toast.makeText(context, result, Toast.LENGTH_LONG).show();
		}
		return isok;
	}

	/**
	 * 如果WIFI是关闭的就打开
	 */
	private static void openWifi() {
		WifiManager mWifiManager = (WifiManager) TApplication.getContext().getSystemService(Context.WIFI_SERVICE);
		mWifiInfo = mWifiManager.getConnectionInfo();
		if(!mWifiManager.isWifiEnabled()){
			mWifiManager.setWifiEnabled(true);
		}
	}

	/**
	 * 获取MAC地址
	 * @return MAC地址
	 */
	private static String getMacAddress(){
		String mac = mWifiInfo.getMacAddress();
		if("02:00:00:00:00:00".equals(mac)){//版本7.0以上
			String str = "";
			String macSerial = "";
			try {
				Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address ");
				InputStreamReader ir = new InputStreamReader(pp.getInputStream());
				LineNumberReader input = new LineNumberReader(ir);

				for (; null != str;) {
					str = input.readLine();
					if (str != null) {
						macSerial = str.trim();// 去空格
						break;
					}
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			if (macSerial == null || "".equals(macSerial)) {
				try {
					return loadFileAsString("/sys/class/net/eth0/address").toUpperCase(Locale.CHINA).substring(0, 17);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return macSerial;
		}
		return (mWifiInfo == null) ? "NULL" : mac;
	}

	private static String loadFileAsString(String fileName) throws Exception {
		FileReader reader = new FileReader(fileName);
		String text = loadReaderAsString(reader);
		reader.close();
		return text;
	}

	private static String loadReaderAsString(Reader reader) throws Exception {
		StringBuilder builder = new StringBuilder();
		char[] buffer = new char[4096];
		int readLength = reader.read(buffer);
		while (readLength >= 0) {
			builder.append(buffer, 0, readLength);
			readLength = reader.read(buffer);
		}
		return builder.toString();
	}
	@SuppressLint("NewApi")
	private static String getWifiMacAddress() {
		String macAddress = null;
		StringBuffer buf = new StringBuffer();
		NetworkInterface networkInterface = null;
		try {
			networkInterface = NetworkInterface.getByName("eth1");
			if (networkInterface == null) {
				networkInterface = NetworkInterface.getByName("wlan0");
			}
			if (networkInterface == null) {
				return "02:00:00:00:00:00";
			}
			byte[] addr = networkInterface.getHardwareAddress();
			for (byte b : addr) {
				buf.append(String.format("%02x:", b));
			}
			if (buf.length() > 0) {
				buf.deleteCharAt(buf.length() - 1);
			}
			macAddress = buf.toString();
		} catch (SocketException e) {
			e.printStackTrace();
			return "02:00:00:00:00:00";
		}
		return macAddress;
	}
}
